package com.mingtu.guards.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.StringUtils;
import com.google.gson.Gson;
import com.hjq.gson.factory.GsonFactory;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.Eventbus.MyEventBus;
import com.mingtu.common.base.BaseFragment;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.common.utils.MyUtills;
import com.mingtu.common.utils.SPTools;
import com.mingtu.common.utils.StatusBarUtil;
import com.mingtu.common.view.MultiGridView;
import com.mingtu.common.view.MyAutofitTextView.AutofitTextView;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.guards.R;
import com.mingtu.guards.activity.ContactsActivity;
import com.mingtu.guards.activity.FileDataActivity;
import com.mingtu.guards.activity.RecordActivity;
import com.mingtu.guards.activity.ReportRecordActivity;
import com.mingtu.guards.activity.ReportedEventLeaderActivity;
import com.mingtu.guards.activity.ResultStatisticsActivity;
import com.mingtu.guards.adapter.MenuAdapter2;
import com.mingtu.guards.bean.IndexTopNumBean;
import com.mingtu.guards.callback.MyStringDialogCallback;
import com.mingtu.guards.utils.MyConstant;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;

public class IndexFragment3 extends BaseFragment implements LazyFragmentPagerAdapter.Laziable {
    @BindView(R.id.grid_view)
    MultiGridView gridView;
    @BindView(R.id.status_bar_view)
    View statusBarView;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_measure_num)
    TextView tvMeasureNum;
    @BindView(R.id.tv_progress)
    TextView tvProgress;
    @BindView(R.id.tv_reported_num)
    TextView tvReportedNum;
    @BindView(R.id.tv_track_mileage_num)
    AutofitTextView tvTrackMileageNum;
    @BindView(R.id.tv_track_time_num)
    AutofitTextView tvTrackTimeNum;
    @BindView(R.id.tv_track_date_num)
    AutofitTextView tvTrackDateNum;
    @BindView(R.id.tv_sign_in_num)
    AutofitTextView tvSignInNum;
    @BindView(R.id.layout_progress)
    LinearLayout layoutProgress;
    @BindView(R.id.layout_event)
    LinearLayout layoutEvent;
    private SmartRefreshLayout srlUp;
    private boolean isLeader = SPStaticUtils.getBoolean(SPTools.isLeader);

    public static IndexFragment3 getInstance() {
        IndexFragment3 fragment = new IndexFragment3();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_index3;
    }

    @Override
    protected void initData() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView(View view) {

        srlUp = (SmartRefreshLayout) view.findViewById(R.id.srl_up);

        MenuAdapter2 menuAdapter = new MenuAdapter2(getContext(), isLeader);
        gridView.setAdapter(menuAdapter);
        gridView.setNumColumns(2);
        menuAdapter.setOnItemClickListener(new MenuOnItenClickListner());


        initListener();
        StatusBarUtil.addStatusBarHeight(statusBarView);

    }

    @Override
    protected void getData() {
        setTime();
        getTopEventNum();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onStringEvent(MyEventBus event) {
        //处理完之后刷新数据
        String message = event.getMessgae();
        if (message.equals(MyConstant.REFRESH_FIRE_NOTICE_DATA)) {

        }

    }

    private void initListener() {
        initPullRefresh();//刷新
    }

    private void initPullRefresh() {
        srlUp.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
        srlUp.setEnableLoadMore(false);
        srlUp.setEnableOverScrollDrag(true);
        srlUp.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                srlUp.finishRefresh(Constant.REFRESH_TIME);
                srlUp.setNoMoreData(false);
                setTime();
                getTopEventNum();
            }
        });
    }

    @OnClick({R.id.layout_progress, R.id.layout_event})
    public void onViewClicked(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.layout_progress:
                IntentUtils.getInstance().readyGo(getActivity(), ResultStatisticsActivity.class);
                break;
            case R.id.layout_event:
                IntentUtils.getInstance().readyGo(getActivity(), ReportedEventLeaderActivity.class);
                break;
        }
    }

    //领导事件
    private class MenuOnItenClickListner implements MenuAdapter2.OnItemClickListener {

        @Override
        public void onItemClick(View view, int position) {
            switch (position) {
                case 0:
                    //文件资料
                    IntentUtils.getInstance().readyGo(getActivity(), FileDataActivity.class);
                    break;
                case 1:
                    //通讯录
                    IntentUtils.getInstance().readyGo(getActivity(), ContactsActivity.class);
                    break;
                case 2:
                    //巡护记录
                    Bundle bundle = new Bundle();
                    bundle.putInt("type", 0);
                    IntentUtils.getInstance().readyGo(getActivity(), RecordActivity.class, bundle);
                    break;
                case 3:
                    //上报记录
                    IntentUtils.getInstance().readyGo(getActivity(), ReportRecordActivity.class);
                    break;

            }

        }
    }


    private void setTime() {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (hour <= 3) {
            tvTime.setText("已经凌晨了，注意休息哦！");
        } else if (hour > 3 && hour <= 6) {
            tvTime.setText("这个点还在工作，注意休息哦！");
        } else if (hour > 6 && hour <= 9) {
            tvTime.setText("早上好！");
        } else if (hour > 9 && hour <= 11) {
            tvTime.setText("上午好！");
        } else if (hour > 11 && hour <= 13) {
            tvTime.setText("中午好！");
        } else if (hour > 13 && hour <= 18) {
            tvTime.setText("下午好！");
        } else if (hour > 18 && hour <= 21) {
            tvTime.setText("晚上好！");
        } else if (hour > 21 && hour < 24) {
            tvTime.setText("夜深了，早点休息哦！");
        }
    }

    /**
     * 顶部数据数字
     */

    public void getTopEventNum() {
        OkGo.<String>post(MyConstant.POST_TASK_STA_TOTAL)
                .tag(this)
                .execute(new MyStringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {

                            Gson gson = GsonFactory.getSingletonGson();
                            IndexTopNumBean indexTopNumBean = new IndexTopNumBean();
                            indexTopNumBean = gson.fromJson(result, IndexTopNumBean.class);
                            IndexTopNumBean.DataBean dataBean = indexTopNumBean.getData();

                            int measureNum = dataBean.getMeasureNum();
                            int progress = dataBean.getProgress();
                            String eventNum = dataBean.getEventNum();
                            int totalMileage = dataBean.getTotalMileage();
                            String duration = dataBean.getTotalDuration();
                            String taskNum = dataBean.getTaskNum();
                            String clockNum = dataBean.getClockNum();

                            try {
                                tvMeasureNum.setText(String.valueOf(measureNum));
                                tvProgress.setText(String.valueOf(progress));
                                tvTrackMileageNum.setText(String.valueOf(totalMileage));


                                if (!StringUtils.isEmpty(eventNum)) {
                                    tvReportedNum.setText(String.valueOf(eventNum));
                                } else {
                                    tvReportedNum.setText("0");
                                }

                                if (!StringUtils.isEmpty(taskNum)) {
                                    tvTrackDateNum.setText(String.valueOf(taskNum));
                                } else {
                                    tvTrackDateNum.setText("0");
                                }

                                if (!StringUtils.isEmpty(clockNum)) {
                                    tvSignInNum.setText(String.valueOf(clockNum));
                                } else {
                                    tvSignInNum.setText("0");
                                }


                                if (!StringUtils.isEmpty(duration)) {
                                    long d = 0;
                                    if (duration.indexOf(".") != -1) {
                                        String array[] = duration.split("\\.");
                                        d = Long.parseLong(array[0]);
                                    } else {
                                        d = Long.parseLong(duration);
                                    }
                                    tvTrackTimeNum.setText(MyUtills.formatSeconds2(d));
                                } else {
                                    tvTrackTimeNum.setText("0");
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    }
                });
    }


}
